package com.example.uniapp.activity.userHome;

import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.graphics.drawable.BitmapDrawable;
import android.graphics.drawable.Drawable;
import android.net.Uri;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.ImageView;
import android.widget.TextView;
import android.widget.Toast;

import androidx.activity.result.ActivityResultCallback;
import androidx.activity.result.ActivityResultLauncher;
import androidx.activity.result.contract.ActivityResultContracts;
import androidx.appcompat.app.AppCompatActivity;
import androidx.appcompat.widget.Toolbar;

import com.example.uniapp.R;
import com.example.uniapp.bean.UserBean;
import com.example.uniapp.dao.UserDao;
import com.example.uniapp.utils.FileImgUtils;
import com.example.uniapp.utils.Tools;

/**
 * 修改个人信息
 */
public class UserUpdateInfoActivity extends AppCompatActivity {
    private ActivityResultLauncher<String> getContentLaucher;
    private Uri uri;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_user_update_info);

        //获取各个组件
        ImageView userImgView = this.findViewById(R.id.user_update_person_info_img);
        TextView userNameTextView = this.findViewById(R.id.user_update_person_info_user_name);
        Button updateButton = this.findViewById(R.id.user_update_person_info_button);
        Toolbar toolbar = this.findViewById(R.id.user_update_person_info_back);

        //设置顶部toolbar点击时间
        toolbar.setNavigationOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                //点击则返回
                finish();
            }
        });

        //获取用户个人信息并加载到对应位置
        UserBean userBean = UserDao.getUserInfoByAccount(Tools.getOnAccount(this));
        userNameTextView.setText(userBean.getUserId());
        //加载头像
        Bitmap imgBitmap = BitmapFactory.decodeFile(userBean.getUserImg());
        userImgView.setImageBitmap(imgBitmap);

        //初始化文件选择器
        getContentLaucher=registerForActivityResult(new ActivityResultContracts.GetContent(), new ActivityResultCallback<Uri>() {
            @Override
            public void onActivityResult(Uri result) {
                if(result!=null) {
                    userImgView.setImageURI(result);
                    uri=result;
                }else {
                    Toast.makeText(UserUpdateInfoActivity.this, "未选择头像", Toast.LENGTH_SHORT).show();
                }
            }
        });

        //按钮点击事件,即提交修改个人信息事件
        updateButton.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                String userName = userNameTextView.getText().toString();
                if(userName.isEmpty()) {
                    Toast.makeText(UserUpdateInfoActivity.this, "用户名不能为空", Toast.LENGTH_SHORT).show();
                }else {
                    //获取新图片并转为二进制
                    Drawable newAvatorDrawable = userImgView.getDrawable();
                    Bitmap newAvatorBitmap = ((BitmapDrawable) newAvatorDrawable).getBitmap();

                    //判断头像是否是原来的图像，如果是那就不用保存，反之则需要重新保存
                    String avatorPath;
                    if(!newAvatorBitmap.equals(imgBitmap)) {
                        avatorPath = FileImgUtils.getImgName(); //获取图片存储路径
                        FileImgUtils.saveImageUriToFileImg(uri, UserUpdateInfoActivity.this, avatorPath); //保存图片
                    }else {
                        avatorPath = userBean.getUserImg();
                    }

                    //更新数据库
                    int result = UserDao.updateUserInfo(userBean.getUserId(), userNameTextView.getText().toString(), avatorPath);
                    if(result == 1) {
                        //运行到这，说明更新成功，返回
                        Toast.makeText(UserUpdateInfoActivity.this, "用户信息更新成功", Toast.LENGTH_SHORT).show();
                        finish();
                    }else {
                        Toast.makeText(UserUpdateInfoActivity.this, "用户信息更新失败", Toast.LENGTH_SHORT).show();
                    }
                }
            }
        });
    }
}